<?php
/*
检验单模块
*/
declare(strict_types=1);

namespace Modules\Purchasing\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Purchasing\Models\Inspectionlist;

use Illuminate\Http\Request;
use Modules\Purchasing\Http\Repositories\PurchaseorderReget;
use Catch\Exceptions\FailedException;
use Illuminate\Support\Facades\View;
use extend\services\warehouse\{warehouseService};//引入自定义类
use Modules\Purchasing\Http\Repositories\InspectionlistReget;
use Modules\Purchasing\Models\Supplierschedule;
use Modules\Purchasing\Models\Supplier;
use Modules\Purchasing\Models\Purchaseorderschedule;
use Modules\Purchasing\Models\Provisionalschedule;
use Modules\Purchasing\Models\Provisional;
use Modules\Purchasing\Models\Purchaseorder;


class InspectionlistController extends Controller
{

    public function __construct(
        protected readonly Inspectionlist $model
    ){}


    /**新的检验列表
     * @param Request $request
     * @return mixed
     */
    public function indexList(Request $request): mixed
    {        

        $data = InspectionlistReget::listtable($request->all());
        $kingDeeService = new warehouseService();
        $datauser = $kingDeeService->uerdata();

        foreach($data as $k=>$v){

            $data[$k]['fmaterialid'] = '';
            $data[$k]['ordernumber'] = '';
           

            $Provisionalschedule = Provisionalschedule::select('purchaseorderschedule_id','provisional_id','inspectionlisttable_id','purchaseorder_id','supplier_id')
            ->where('inspectionlisttable_id',$v['id'])
            ->groupby(['provisional_id','inspectionlisttable_id','purchaseorderschedule_id','purchaseorder_id','supplier_id'])
            ->get()
            ->toArray();
            $provisional_id = '';
            $fmaterialidName = '';
            $fbillno = '';
            $purchaseorderschedule_id = [];
            if(!empty($Provisionalschedule)){
                
                    foreach($Provisionalschedule as $ks=>$vs){
                        $provisional_id = $vs['provisional_id'];
                        $purchaseorderschedule_id[] = $vs['purchaseorderschedule_id'];
                        // $purchaseorder_id[] = $vs['purchaseorder_id'];
                        
                    }
                    $purchaseorderschedule_id = array_unique($purchaseorderschedule_id);
                    $fmaterialid = Purchaseorderschedule::select('purchaseorderschedule.fmaterialid','purchaseorder.fbillno')
                    ->join('purchaseorder as purchaseorder', 'purchaseorder.id', '=', 'purchaseorderschedule.purchaseorder_id')
                    ->whereIn('purchaseorderschedule.id',$purchaseorderschedule_id)
                    // ->whereIn('purchaseorder.purchaseorder_id',$purchaseorder_id)
                    ->get()
                    ->toArray();
                    
                    if(!empty($fmaterialid)){
                        foreach ($fmaterialid as $kk=>$vv){
                              $fbillno .= $vv['fbillno'].'、';
                             $fmaterialidName .= $vv['fmaterialid'].'、';
                        }
                    }
                    $fbillno = rtrim($fbillno,'、');
                    $fbillno = explode('、',$fbillno);
                    $fbillno = array_unique($fbillno);
                    $fbillnodata='';
                    foreach ($fbillno as $k1=>$v1){
                        $res = mb_convert_encoding($v1, 'UTF-8', 'UTF-8'); 
                        $fbillnodata .=$res .'、';
                    }
                    $data[$k]['fbillno'] = rtrim($fbillnodata,'、');
                    
                    $fmaterialidName = rtrim($fmaterialidName,'、');
                    $explode = explode('、',$fmaterialidName);
                    $explode = array_unique($explode);
                    $fmaterialidNamedata='';
                    foreach ($explode as $k1=>$v1){
                        $res = mb_convert_encoding($v1, 'UTF-8', 'UTF-8'); 
                        $fmaterialidNamedata .=$res .'、';
                    }
                    $data[$k]['fmaterialid'] = rtrim($fmaterialidNamedata,'、');

            
                $supplierOrder = Supplier::select('supplierOrder')->where('id',$Provisionalschedule[0]['supplier_id'])->first();
                $data[$k]['supplierOrder'] = $supplierOrder['supplierOrder']; 
            }

            $Provisional = Provisional::select('ordernumber')
            ->where('id',$provisional_id)
            ->first();
            
            if(!empty($Provisional)){
                $data[$k]['ordernumber'] = $Provisional->ordernumber;//暂收单
            }
            $data[$k]['provisional_id'] = $provisional_id;
            $data[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]['username']:'';
            switch ($v['state']){
                case 0:
                    $data[$k]['description'] = '待检验'; 
                break;
                case 1:
                    $data[$k]['description'] = '已检验'; 
                break;
            }
        }

        return $data;


    }



    /**旧的类列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {            
      
        $data = InspectionlistReget::list($request->all());
        $kingDeeService = new warehouseService();
        $datauser = $kingDeeService->uerdata();
           
        if(empty($data)){
            return $data=['code'=>1000,'message'=>'请求为空','data'=>$data];
        }
        foreach($data as $k=>$v){
            
            $data[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]['username']:'';
            $data[$k]['inspector_user'] = isset($datauser[$v['inspector_user']])?$datauser[$v['inspector_user']]['username']:'';
            $data[$k]['auditor_user'] = isset($datauser[$v['auditor_user']])?$datauser[$v['auditor_user']]['username']:'';
            
            switch ($v['state']){
                case 0:
                    $data[$k]['description'] = '待审核'; 
                break;
                case 1:
                    $data[$k]['description'] = '已审核'; 
                break;
            }

            $Supplierschedule = Supplierschedule::
            select('purchaseorderschedule_id')
            ->where('id',$v['supplierschedule_id'])
            ->groupby(['supplier_id','purchaseorder_id','purchaseorderschedule_id']) 
            ->first();

            $purchaseorderschedule = purchaseorderschedule::where('id',$Supplierschedule['purchaseorderschedule_id'])->first()->toArray();
           
            $data[$k]['fmaterialid'] = $purchaseorderschedule['fmaterialid'];
            $data[$k]['fmaterialname'] = $purchaseorderschedule['fmaterialname'];
            $data[$k]['fmaterialmodel'] = $purchaseorderschedule['fmaterialmodel'];
              
            $data[$k]['fmodel'] = $purchaseorderschedule['fmodel'];
            $data[$k]['funitid_number'] = $purchaseorderschedule['funitid_number'];
            $data[$k]['funitid_name'] = $purchaseorderschedule['funitid_name'];
             
        }

        return $data;
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {

        $data = $request->all();
        $data['creator_id'] = $this->getLoginUserId();//当前登录用户名称
        return InspectionlistReget::create($data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {   
        $datares = InspectionlistReget::showApi($id);
        return  $datares;
    }

    /**修改接口
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function updateCheck(Request $request)
    {   
        $data = $request->all();
        $data['creator_id'] = $this->getLoginUserId();//当前登录用户名称
        return InspectionlistReget::createUpdate($data);
    }


    
    /* 添加接口
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function Dataaddition(Request $request)
    { 

        $data = $request->all();
        $data['creator_id'] = $this->getLoginUserId();//当前登录用户名称
        return InspectionlistReget::Dataadditiondate($data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return InspectionlistReget::delete($id);
    }

   

     /*检验单PDA扫码
     * @param $id
     * @return mixed
     */
    public function checkPda($code,Request $request)
    {
      
        $data = $request->all();

        return InspectionlistReget::checkPdaApi($code,$data);
    }
    
    /*审核接口
    * @param $id
    * @return mixed
    */
    public function examinePda($id)
    {
        return InspectionlistReget::auditupdate($id);
    }


    
    /*不合格退货单-列表
    * @param $id
    * @return mixed
    */
    public function Belowstandard(Request $request)
    {
        return InspectionlistReget::auditupdate($id);
    }

  /*检验单整单提交校验
    * @param $id
    * @return mixed
    */
    public function checksingle($id)
    {
        
        return InspectionlistReget::checksingledata($id);
    }

  

}
